<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="../../css/admin/style.css" rel="stylesheet" type="text/css" />
    <script src="../../js/jquery-1.8.3.min.js" type="text/javascript"></script >
    <script src="../../js/ajaxfileupload.js" type="text/javascript"></script>
    <script src="../../js/ckeditor/ckeditor.js" type="text/javascript"></script >
    <script src="../../js/ckfinder/ckfinder.js" type="text/javascript"></script>
    <script>
        var editor
        $(function (){
            editor = CKEDITOR.replace("content");
            CKFinder.setupCKEditor(editor,'../../js/ckfinder');
            $.get("../../newstype/getnewtpyeList",function (res){
                console.log(res);
                if (res.code == 202){
                    window.parent.location.replace("../login.html");
                }else if (res.code == 500){
                    alert(res.msg);
                }else if (res.code == 201){
                    alert(res.msg);
                }else {
                    var str = "";
                    for (i = 0; i < res.data.length; i++) {
                        str += "<option value='" + res.data[i].id +"'>" + res.data[i].journame + "</option>";
                    }
                    $("#journame").append(str);
                }
            })
        })

        function fileUpload(){
            $.ajaxFileUpload({
                    url: '../../writer/fileUpload', //用于文件上传的服务器端请求地址  调用后端接口(interface)
                    fileElementId: 'fileID', //文件上传域的ID
                    dataType: 'json', //返回值类型 一般设置为json
                    success: function (data){
                        console.log(data);
                        if (data.code ==200){
                            var imgsrc = "http://localhost:8080/userImg/"+data.data;
                            $("#coverimage1").attr("src",imgsrc);
                            var url1 = data.data.split("/")[1];
                            $("#coverimage").val(url1);
                        }else if (data.code = 500){
                            console.log(data.msg);
                        }
                    }
                }
            )
        }
        function savenews(state){
            var title = $("#title").val();
            var summary = $("#summary").val();
            var coverimage = $("#coverimage").val();
            var journame = $("#journame").val();
            var content = editor.getData();
            $.get("../../writer/savenews",{title:title,summary:summary,coverimage:coverimage,journame:journame,content:content,state:state},function (res){
                if (res.code == 202){
                    window.parent.location.replace("../login.html");
                }else if (res.code == 500){
                    alert(res.msg);
                }else {
                    alert(res.msg)
                    goback();
                }
            })
        }


        function goback(){
            location.replace("list.html");
        }

    </script>

</head>
<body>
<form id="formId">
    <input type="hidden" id="id" name="id">
    <div class="formbody">
        <div class="formtitle"><span>基本信息</span></div>
        <ul class="forminfo">
            <li>
                <label>标题</label>
                <input id="title" name="title" type="text" class="dfinput" />
            </li>
            <li>
                <label>摘要</label>
                <input id="summary" name="summary" type="text" class="dfinput" />
            </li>
            <li>
                <label>封面图</label>
                <img src="" id="coverimage1" width="80px" height="40px">
                <input type="hidden" id="coverimage" name="coverimage">
                更换头像<input type="file" name="fileName" accept=".jpg,.png,.gif" id="fileID">
                <input type="button" value="上传" onclick="fileUpload()">
            </li>
            <li>
                <label>新闻类型</label>
                <select id="journame" name="journame" class="dfinput">
                    <option>请选择</option>
                </select>
            </li>
            <li>
                <label>文章内容</label>
                <textarea cols="100" rows="40" id="content" name="content"></textarea>

            </li>
            <li>
                <input type="button" value="保存" onclick="savenews(0)" />
                <input type="button" value="提交" onclick="savenews(1)" />
                <input type="button" value="返回" onclick="goback()" />
            </li>
        </ul>
    </div>
</form>
</body>
</html>